package com.scrx.ymg.service.product;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.scrx.ymg.dao.product.IProductDao;
import com.scrx.ymg.entity.AgentHot;
import com.scrx.ymg.entity.AgentSuperMarketSet;
import com.scrx.ymg.entity.HeadEntrepot;
import com.scrx.ymg.entity.SuperMarketEntrepot;
import com.scrx.ymg.entity.SupermarketSet;
import com.scrx.ymg.util.BaseUtil;
import com.scrx.ymg.util.LatLonUtil;

@Service("productService")
public class ProductServiceImpl implements IProductService {
     
	@Autowired
	private IProductDao   productDao;

	public String save(HeadEntrepot entrepot) {
		
		return productDao.save(entrepot);
	}


	public String save(HeadEntrepot head, MultipartFile file,MultipartFile bigFile,MultipartFile detailFile) {
		head.getBarCode();
		String msg=productDao.whetherToRepeat(head);
		if (msg.equals("250")) {
			return msg;
		}
		String Path=BaseUtil.goodsPath;
		String filename=BaseUtil.addRandom(file.getOriginalFilename());
        String filePath =Path+ filename;
        head.setProductSimg(filename);
        
        String bigFileName=BaseUtil.addRandom(bigFile.getOriginalFilename());
        String bigfilePath =Path+ bigFileName;
        head.setProductBimg(bigFileName);
        
        String detailFileName=BaseUtil.addRandom(detailFile.getOriginalFilename());
        String detailFilePath="";
        if(detailFile.getSize()>0){
        	 detailFilePath =Path+ detailFileName;
             head.setDetailImg(detailFileName);
        }
        
        head.setChecka(1);
         msg=productDao.save(head,file,filePath,bigFile,bigfilePath,detailFile,detailFilePath);
		return msg;
	}


	public Map<Object, Object> selectByCondition(HeadEntrepot head, int pageNum, int pageCount) {
	
		return productDao.selectByCondition(head, pageNum, pageCount);
	}



	public String putaway(SuperMarketEntrepot superEnterpot, int headID) {
		//1. 得到总部商品
		HeadEntrepot head=productDao.selectHeadEntrepotByID(headID);
		//2.判断超市是否已拥有商品.
		SuperMarketEntrepot old = productDao.selectBySuperId(superEnterpot.getSuperId(),superEnterpot.getBarCode());
		String temp="false";
		if(old!=null){
			old.setReferenceValue(superEnterpot.getReferenceValue());
			old.setPromotionPrice(superEnterpot.getPromotionPrice());
			old.setDiscountPrice(superEnterpot.getDiscountPrice());
			old.setRepertory(superEnterpot.getRepertory());
			old.setRecommend(superEnterpot.getRecommend());
			old.setRecommendTime(BaseUtil.newdate());
			//2.1已有则更新覆盖
			temp=productDao.update(old);
		}else{
			//2.2没有则新建
			superEnterpot.setBarCode(head.getBarCode());
			superEnterpot.setProductSimg(head.getProductSimg());
			superEnterpot.setProductBimg(head.getProductBimg());
			superEnterpot.setSuperDetailImg(head.getDetailImg());
			superEnterpot.setInactive1(head.getGoodsType());
			superEnterpot.setHot(0);
			superEnterpot.setSales(0);
			superEnterpot.setPutaway(1);
			superEnterpot.setRecommendTime(BaseUtil.newdate());
			superEnterpot.setInactive3(0);
			if(superEnterpot.getPromotionPrice()==null||superEnterpot.getPromotionPrice().intValue()<=0){
				superEnterpot.setPromotionPrice(0.0);
			}
			temp=productDao.save(superEnterpot);
		}
		return temp;
	}



	public Map<Object, Object> selectByCondition(SuperMarketEntrepot head, int pageNum, int pageCount) {

		return productDao.selectByCondition( head,  pageNum,  pageCount);
	}



	public String saveorupdateSupermarketSet(AgentSuperMarketSet sueprset) {
		AgentSuperMarketSet set = productDao.getAgentSuperMarketSetBySuperID(sueprset.getSupermarketId());
	    if(set!=null){
	    	set.setUpSendMoney(sueprset.getUpSendMoney());
	    	set.setFreeSendMoney(sueprset.getFreeSendMoney());
	    	set.setSendMoney(sueprset.getSendMoney());
	    	set.setSendPeople(sueprset.getSendPeople());
	    	set.setSendPhone(sueprset.getSendPhone());
	    	set.setSendTime(sueprset.getSendTime());
	    	set.setSuperName(sueprset.getSuperName());
	    	set.setAgentId(sueprset.getAgentId());
	    	set.setStatus(1);
	    	set.setAfterService(sueprset.getAfterService());
	    	return productDao.updateAgentSuperMarketSet(set);
	    }
		return productDao.saveAgentSuperMarketSet(sueprset);
	}



	public String updateSuperMarketEntrepot(SuperMarketEntrepot supermarket) {
		SuperMarketEntrepot enter=productDao.selectByID(supermarket.getId());
		if(enter!=null){
			if(supermarket.getRecommend()!=null){//推荐
				enter.setRecommend(supermarket.getRecommend());
				enter.setRecommendTime(BaseUtil.newdate());
			}
			if(supermarket.getPutaway()!=null){//上架
				enter.setPutaway(supermarket.getPutaway());
			}
			if(supermarket.getDiscountPrice()!=null){//折扣价
				enter.setDiscountPrice(supermarket.getDiscountPrice());
			}
			if(supermarket.getReferenceValue()!=null){//零售价
				enter.setReferenceValue(supermarket.getReferenceValue());
			}
			if(supermarket.getPromotionPrice()!=null){//促销价
				enter.setPromotionPrice(supermarket.getPromotionPrice());
			}
			if(supermarket.getRepertory()!=null){//库存数量
				enter.setRepertory(supermarket.getRepertory());
			}
			if(supermarket.getInactive3()!=null){//爆款库存数量
				enter.setInactive3(supermarket.getInactive3());
			}
			return productDao.update(enter);
		}else{
			return "false";
		}
	}
	


	public String deleteSuperMarketEntrepot(SuperMarketEntrepot supermarket) {
		SuperMarketEntrepot enter = productDao.selectByID(supermarket.getId());
		return productDao.delete(enter);
	}



	public SupermarketSet getSupermarketSet(int superID) {
	
		return productDao.getSupermarketSetBySuperID(superID);
	}



	public HeadEntrepot updateSuperMarketEntrepot(int headId) {
		return productDao.selectHeadEntrepotByID(headId);
	}



	public String updateHeadProduct(HeadEntrepot head, MultipartFile file,MultipartFile bigFile,MultipartFile detailFile) {
		LatLonUtil util = new LatLonUtil();
		String goodsPath = BaseUtil.goodsPath;
		String filePath="";
		String bigFilePath="";
		String detailFilePath="";
		HeadEntrepot oldHead = productDao.selectHeadEntrepotByID(head.getId());
		if(file.getSize()>0){
			String fileName=BaseUtil.addRandom(file.getOriginalFilename());
			filePath = goodsPath + fileName;
			head.setProductSimg(fileName);
		}else{
			head.setProductSimg(oldHead.getProductSimg());
		}
		if(bigFile.getSize()>0){
			String bigFileName=BaseUtil.addRandom(bigFile.getOriginalFilename());
			bigFilePath = goodsPath + bigFileName;
			head.setProductBimg(bigFileName);
		}else{
			head.setProductBimg(oldHead.getProductBimg());
		}
		if(detailFile.getSize()>0){
			String detailFileName=BaseUtil.addRandom(detailFile.getOriginalFilename());
			detailFilePath = goodsPath + detailFileName;
			head.setDetailImg(detailFileName);
		}else{
			head.setDetailImg(oldHead.getProductBimg());
		}
		head.setChecka(0);
		return productDao.update(head,file,filePath,bigFile,bigFilePath,detailFile,detailFilePath);
	}


	public String updateSupermarketSet(SupermarketSet sueprset) {
		return productDao.updateSupermarketSet(sueprset);
	}

}
